<?php
function canvas_admin() {
	global $wpdb, $canvas, $main_page;
	$main_page = '';
	if(get_option('canvas_auto_reload') == 'true') canvas_reload();
?>
<div id="toggle-header">
<span id="show_content" style="display: inline;"><a href="javascript:void(0)"><img src="<?php echo CANVASURI; ?>images/show_header.gif" alt="Show" />Show the menu</a></span>
<span id="hide_content" style="display: none;"><a href="javascript:void(0)"><img src="<?php echo CANVASURI; ?>images/hide_header.gif" alt="Hide" />Make more room</a></span>
</div>

<div id="header-top">
</div>
<div id="header-bottom">
</div>

<?php canvas_admin_menu(); ?>

<?php if($wpdb->get_var("SHOW TABLES LIKE '".$canvas->main."'") == $canvas->main) : ?>
<div id="canvas">
<?php
	if($_GET["content"] == 'main') canvas_admin_main($main_page);
	elseif($_GET["content"] == 'ink' && function_exists('ink_admin')) ink_admin();
	elseif($_GET["content"] == 'manage') canvas_plugin_manager();
	elseif($_GET["content"] == 'options') canvas_options();
	elseif($_GET["content"] == 'tips') canvas_help();
	else canvas_admin_main($main_page);
?>
</div>

<?php
else : echo '<div class="content_wrap"><strong>Error:</strong> the Canvas tables don\'t exist in your database. Please try reinstalling.</div>';
endif;
}





function canvas_admin_main($main_page) {
	if($main_page == '') $main_page = 'home'; // Attempt to find the main page
?>
<input type="hidden" id="canvas_page" name="canvas_page" value="<?php echo $main_page; ?>" />
<div class="content_wrap">
<?php if(get_option('canvas_show_instructions') == 'true') : ?><div id="instructions">Drag and drop the blocks from your shelf to the canvas to create your own layout. You can select the page type to build from the menu above. To use a block in more than one place, you must <a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=manage">duplicate</a> it first. To save your work, press the "Publish Changes" button. <small><a href="javascript:void(0)" id="hide_instructions">don't show this message again</a> &times;</small></div><?php endif; ?>

<?php canvas_build_canvas(); ?>

</div>

<!-- Shelf -->
<div class="shelf_column">
	<div class="titlebar">
	<h5 class="title"><?php if(get_option('canvas_show_shelf_tooltip') == 'true') : ?>Your Shelf <span>&#8212; Contains available blocks that you haven't already placed in your layout</span><?php endif; ?>	</h5>

	<a href="javascript:void(0)"><img id="other" src="<?php echo CANVASURI; ?>images/group_other.png" alt="Other"></a>
	<a href="javascript:void(0)"><img id="services" src="<?php echo CANVASURI; ?>images/group_services.png" alt="Services"></a>
	<a href="javascript:void(0)"><img id="wordpress" src="<?php echo CANVASURI; ?>images/group_wordpress.png" alt="Wordpress"></a>
	<a href="javascript:void(0)"><img id="posts" src="<?php echo CANVASURI; ?>images/group_posts.png" alt="Posts"></a>
	<a href="javascript:void(0)"><img id="all" src="<?php echo CANVASURI; ?>images/group_all.png" alt="All"></a>
	</div>
	<span class="spaceholder"></span>
		<div class="canvas_droppable_zone" id="shelf">
			<?php get_canvas_draggables('shelf', $_GET['template']); ?>
		</div>
</div>

<?php } 

function canvas_plugin_manager() {
	global $wpdb, $canvas;
	$blocks = $wpdb->get_results("SELECT * FROM ".$canvas->main." WHERE theme = '".get_option('template')."' ORDER BY block_name");
?>
<div class="content_wrap">
<p><strong>Your database currently contains <span id="count"><?php echo count($blocks); ?></span> Canvas blocks and plugins. Using this management page you can duplicate, delete, and restore block information.</strong></p>
<p>
<a href="javascript:void(0)" onclick="CanvasManage.sortGroups('all')"><img src="<?php echo CANVASURI; ?>images/group_all.png" alt="all"/></a>  
<a href="javascript:void(0)" onclick="CanvasManage.sortGroups('wordpress')"><img src="<?php echo CANVASURI; ?>images/group_wordpress.png" alt="wordpress" title="Wordpress"/></a> 
<a href="javascript:void(0)" onclick="CanvasManage.sortGroups('posts')"><img src="<?php echo CANVASURI; ?>images/group_posts.png" alt="posts" title="Posts"/></a> 
<a href="javascript:void(0)" onclick="CanvasManage.sortGroups('services')"><img src="<?php echo CANVASURI; ?>images/group_services.png" alt="services" title="Services"/></a> 
<a href="javascript:void(0)" onclick="CanvasManage.sortGroups('other')"><img src="<?php echo CANVASURI; ?>images/group_other.png" alt="other" title="Other"/></a> 
</p>
<ul class="manage" id="manage">
	<?php 
	if(!empty($blocks)) {
		foreach($blocks as $block) {
			canvas_create_management_block($block, '', get_option('canvas_allow_deletion'));
		}
	}
	?>
</ul>
<div id="help">
<span id="message"></span>
<h3>Installing New Content</h3>
	<p>You can <a class="dbmanage" id="rescan" href="javascript:void(0)">rescan</a> your Canvas directories to install new content blocks for your blog. You change your Canvas <a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=options">options</a> to make this happen automatically.</p>
	<p>You can also download new content or share your own work at the Canvas <a href="">plugin clearinghouse</a>.</p>
<h3>Deleting Blocks</h3>
	<p>If you no longer want a block to live in your database, you can give it the boot by deleting it. The block files will still be available in your Canvas directories unless you choose to delete them as well. Be aware that deleting plugin and block files before removing them from the database could produce errors.</p>
<h4>"HELP! I deleted a block that I didn't want to. How do I get it back?"</h4>
	<p>Don't worry, it happens to the best of us. If you haven't deleted the files themselves, you should be able to <a class="dbmanage" id="rescan" href="javascript:void(0)">rescan and install</a> all of your blocks and plugins without losing saved data.</p>
	<p>If that's unsuccessful, you can try <a class="dbmanage" id="reinstall" href="javascript:void(0)">reinstalling</a> Canvas from scratch. Be careful, this will <em>permanently</em> erase any layout changes you have made.</p>

<?php if(get_option('canvas_show_rss_tips') == 'true') : ?>
<?php
	require_once(ABSPATH . WPINC . '/rss-functions.php');
	$rss = @fetch_rss('http://www.freshpursuits.com/canvas/tips.xml');
	if(isset($rss->items) && count($rss->items) != 0) : ?>
	<h3>Canvas Tips <a href="http://www.freshpursuits.com/canvas/tips.rss"><img src="<?php echo CANVASURI ?>images/FeedIcon.png" alt="RSS"/></a></h3>
	<ul class="rssfeed">
	<?php
	$rss->items = array_slice($rss->items, 0, $vars['number']);
	foreach ($rss->items as $item ) : ?>
		<li><a href="<?php echo wp_filter_kses($item['link']); ?>"><?php echo wp_specialchars($item['title']); ?></a></li>
	<?php endforeach; ?>
	</ul>
	<?php endif; ?>
<?php
	$rss = @fetch_rss('http://www.freshpursuits.com/canvas/news.xml');
	if(isset($rss->items) && count($rss->items) != 0) : ?>
	<h3>Canvas News <a href="http://www.freshpursuits.com/canvas/news.rss"><img src="<?php echo CANVASURI ?>images/FeedIcon.png" alt="RSS"/></a></h3>
	<ul class="rssfeed">
	<?php
	$rss->items = array_slice($rss->items, 0, $vars['number']);
	foreach ($rss->items as $item ) : ?>
		<li><a href="<?php echo wp_filter_kses($item['link']); ?>"><?php echo wp_specialchars($item['title']); ?></a></li>
	<?php endforeach; ?>
	</ul>
	<?php endif; ?>
</div>
<?php endif; ?>

</div>
<?php } 





function canvas_options() {
	global $wpdb, $canvas;
?>
<div class="content_wrap">
<div id="message"></div>
<form class="options" id="options">
	<h4>Global Elements</h4>
	<label><input type="checkbox" name="canvas_hide_menu" id="canvas_hide_menu" value="1" <?php canvas_is_checked('canvas_hide_menu') ?>/> Automatically hide the Wordpress Admin when working in Canvas</label>
	<label><input type="checkbox" name="canvas_auto_reload" id="canvas_auto_reload" value="1" <?php canvas_is_checked('canvas_auto_reload') ?>/> Rescan and install new blocks and plugins each time the Canvas Layout page is loaded</label>
	<h4>Interface Elements</h4>
	<label><input type="checkbox" name="canvas_auto_publish" id="canvas_auto_publish" value="1" <?php canvas_is_checked('canvas_auto_publish') ?>/> Automatically update your blog with each drop to the canvas (if unchecked, the "Publish" button must be clicked to save changes)</label>
	<label><input type="checkbox" name="canvas_allow_deletion" id="canvas_allow_deletion" value="1" <?php canvas_is_checked('canvas_allow_deletion') ?>/> Show the delete button for blocks on the Block Management page (leave unchecked to prevent accidental deletion)</label>
	<label><input type="checkbox" name="canvas_resize_shelf" id="canvas_resize_shelf" value="1" <?php canvas_is_checked('canvas_resize_shelf') ?>/> Make the Canvas shelf resizable (beta feature, may behave badly) (credit to <a href="http://blog.craz8.com/articles/2005/12/01/make-your-divs-resizeable">Tom</a>)</label>
	<label><input type="checkbox" name="canvas_hide_zone_options" id="canvas_hide_zone_options" value="1" <?php canvas_is_checked('canvas_hide_zone_options') ?>/> Hide zone options on page load</label>
	<h4>Help Elements</h4>
	<label><input type="checkbox" name="canvas_show_zone_name" id="canvas_show_zone_name" value="1" <?php canvas_is_checked('canvas_show_zone_name') ?>/> Show zone names on droppable zones</label>
	<label><input type="checkbox" name="canvas_show_rss_tips" id="canvas_show_rss_tips" value="1" <?php canvas_is_checked('canvas_show_rss_tips') ?>/> Show Canvas News and Tips on the Block Management page</label>
	<label><input type="checkbox" name="canvas_show_shelf_tooltip" id="canvas_show_shelf_tooltip" value="1" <?php canvas_is_checked('canvas_show_shelf_tooltip') ?>/> Enable the tooltip in the Canvas shelf</label>
	<label><input type="checkbox" name="canvas_show_instructions" id="canvas_show_instructions" value="1" <?php canvas_is_checked('canvas_show_instructions') ?>/> Display the instructions on the Canvas Layout page</label>
	<div id="save_button"><input type="button" id="save_options" name="save_options" value="Save Options"/></div>
</form>
</div>
<?php } 

function canvas_is_checked($option_name='') {
	if(get_option($option_name) == 'true') echo 'checked="checked"';
}





function canvas_help() {
	global $wpdb, $canvas;
?>
<div class="content_wrap">
	<h3>Welcome to Canvas</h3>
	<p>Canvas is designed to provide bloggers and website administrators the tools to quickly and effectively create beautiful content-rich experiences. Here are a few FAQs and tips to help you develop your Canvas blog to it's full potential.</p>
	<p>Many more tips and Canvas information are available at the official <a href="http://www.freshpursuits.com/canvas/">website</a>.</p>
	<h3>We Love Being Helpful</h3>
		<p>While we've tried to make Canvas intuitive and simple, we know it may not be simple for everyone. That's why we check the <a href="http://forums.freshpursuits.com/">forums</a> almost daily to help our users. You'll get the answers you need, and you'll help us make Canvas easier to use by asking us questions. Everyone wins!</p>
		<p><a href="http://forums.freshpursuits.com/">Learn more &raquo;</a></p>
	<ol class="tips">
		<li>How do I put the same block in more than one place?<p>You can duplicate blocks using the <a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=manage">Block Management</a> pane of Canvas. Simply push the "Duplicate Block" button on the block you'd like to copy. The new block will show up in your Canvas shelf and can be used anywhere &#8212; completely independent of the original.</p></li>
		<li>How do I delete blocks?<p>You can delete blocks using the <a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=manage">Block Management</a> pane. When you push the "Delete Block" button on the block you'd like to delete, Canvas will delete the database entry. To permanently remove the block,  you should also delete the corresponding PHP and .canvas files from your server. If you choose to leave these files, the plugin or block can be reinstalled by using the "rescan" feature in the Block Management pane.</p></li>
		<li>I deleted a block I didn't want to delete. How do I get it back?<p>Follow the instructions in the <a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=manage">Block Management</a> pane to rescan or reinstall Canvas.</p></li>
		<li>I've inserted a block but I get errors on my webpage. Why?<p>Chances are, your database isn't up to date with the content on your server (deleting a block file without removing it using the Management pane will cause this, for instance). Head over to the <a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=manage">Block Management</a> pane and choose "rescan." There's also an <a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=options">option</a> that will do this automatically for you.</p><p>If your problem persists, there's probably an error in the plugin or block itself. You should contact the author to resolve the issue.</p></li>
		<li>My delete button doesn't show up. Where is it?<p>You've probably deactivated the delete button. You can reactivate the button from the <a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=options">Canvas Options</a> pane.</p></li>
		<li>Ink doesn't seem to register my changes. What's the deal?<p>Like most of Canvas, Ink uses AJAX to update your database. Ink is a bit abusive, though. Updates are only sent "on change," which means your browser has to detect that the form value has changed somehow. Depending on your browser, you may have to change focus &#8212; either by clicking on another field or by clicking somewhere else on the page &#8212; before your browser will send the request.</p></li>
		<li>Canvas won't play nice with my browser.<p>That's not a question. If it had been, this would be the answer: Canvas is designed for <a href="http://www.getfirefox.com/">Firefox</a> and may not agree with your browser's Javascript rendition. Future versions of Canvas will be more cross-browser compatible than the current build.</p></li>
		<li>What powers Canvas?<p>Mountain Dew and Oreos. And a handful of Javascript libraries (namely <a href="http://prototype.conio.net/">Prototype</a> by <a href="http://conio.net/">Sam Stephenson</a> and <a href="http://script.aculo.us/">Script.aculo.us</a> by <a href="http://mir.aculo.us/">Tom Fuchs</a>).</p></li>
	</ol>
	<h3>System Info</h3>
	<p>Canvas: <a href="<?php echo get_option('canvas_uri'); ?>">v<?php echo get_option('canvas_version'); ?></a><br/>PHP: <?php echo phpversion(); ?><br/>MySQL: <?php echo $wpdb->get_var("SELECT VERSION()"); ?></p>
</div>
<?php } 

function canvas_admin_menu() {
	global $main_page;
?>	<div id="canvas_dropdown">
<?php	if($_GET["page"] == 'canvas.php' && (!isset($_GET["content"]) || $_GET["content"] == 'main')) :
			if(isset($_GET["template"])) $template = $_GET["template"];
				else $template = get_option('template');
			$filename = ABSPATH.'/wp-content/themes/'.$template.'/canvas.php';
			if(file_exists($filename)) {
				$file_data = implode('', file($filename));
				preg_match_all("|<!-- Canvas Page:(\s*)(.*) -->|", $file_data, $pages);
			}
?>
	<ul class="page_dropdown">
		<li id="top_menu"><a href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php">Select a Canvas</a>
		<div class="dropdown_menu" id="dropdown_menu">
		<ul>
<?php 		
		if(!empty($pages[2])) {
			foreach($pages[2] as $page) {
				list($page_name, $id) = split('\s*\|\s*',$page);
				if($main_page == '') $main_page = trim($id);
				echo '<li><a id="'.trim($id).'__menu" href="javascript:void(0)">'.trim($page_name).'</a></li>';
			}
		} else {
			echo '<li><a href="javascript:void(0)">Please select a Canvas compatible theme below</a></li>';
		} ?>
		</ul>
		</div>
		</li>
	</ul>
	<?php else : ?>
	<a class="canvas_menu_item" href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php">Canvas Layout Manager</a>
	<?php endif; ?>
	<a class="canvas_menu_item" href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=manage">Block Management</a>
	<a class="canvas_menu_item" href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=ink">Ink Color Palette</a>
	<a class="canvas_menu_item" href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=options">Canvas Options</a>
	<a class="canvas_menu_item" href="<?php bloginfo('wpurl') ?>/wp-admin/themes.php?page=canvas.php&content=tips">Tips and Troubleshooting</a>
	</div>

<?php

} ?>